import { Game } from "../../../Game";
import { GameConst } from "../../../../component/gconst/GameConst";
import { FilterConst } from "../../../../component/gconst/FilterConst";
import { GameMapUtil } from "../../../scene/map/GameMapUtil";
import { MapNodeType } from "../../../scene/map/MapNodeType";
import { ThingUtil } from "../../../scene/layer/utils/ThingUtil";
import { SceneRange } from "../../../scene/map/SceneRange";
import { EntityType } from "../../../rules/EntityType";
import { BossRule } from "../../../rules/BossRule";
import { SWFPlayer } from "../../../scene/modle/SWFPlayer";
import { TCrossDefenseUpgrade } from "../../../../../Message/Db/Tables/TCrossDefenseUpgrade";
import { Cache } from "../../../cache/Cache";
import { BossConfig } from "../../../resource/BossConfig";
import { ModelUtil } from "../../../scene/player/info/ModelUtil";
import { ModelType } from "../../../scene/modle/data/ModelType";
import { LayerManager } from "../../../manager/LayerManager";
import { DisplayUtil } from "../../../../common/DisplayUtil";
import { MonsterPlayer } from "../../../scene/player/entity/MonsterPlayer";
import { CrossDefenceTowerInfo } from "./data/CrossDefenceTowerInfo";
import { CrossDefenceCopyUpgradeMenu } from "./item/CrossDefenceCopyUpgradeMenu";
import { MsgManager } from "../../../manager/MsgManager";
import { Language } from "../../../../../extend/language/Language";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../events/DataEvent";
import { EventName } from "../../../mvc/EventName";
type int = number;
//class CrossDefenceCopyTowerBuilder
    
    export  class CrossDefenceCopyTowerBuilder
    {
        constructor()
        {
            
            this._buidingLayer = Game.scene.bulidlayer;
            return;
        }

        /* internal  */isBuildabled(): Boolean
        {
            return true;
        }

        /* internal  */mouseMoveHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            this.bodyPlayer.sceneX = this._buidingLayer.mouseX;
            this.bodyPlayer.sceneY = this._buidingLayer.mouseY;
            this.towerRoundEffect.x = this._buidingLayer.mouseX;
            this.towerRoundEffect.y = this._buidingLayer.mouseY;
            this.towerAttackRound.x = this._buidingLayer.mouseX;
            this.towerAttackRound.y = this._buidingLayer.mouseY;
            if (!this.isTowerPoint(Game.scene.mouseX, Game.scene.mouseY) || this.isOtherTowerAround(new Object /* flash.geom.Point */(Game.scene.mouseX, Game.scene.mouseY), GameConst.CrossDefenseTowerDistance)) 
            {
                this.towerRoundEffect.filters = [FilterConst.colorFilterRed];
            }
            else 
            {
                this.towerRoundEffect.filters = [];
            }
            return;
        }

        /* internal  */isTowerPoint(arg1: number, arg2: number): Boolean
        {
            var loc3=0;
            var loc4=0;
            var loc5=0;
            var loc6=0;
            var loc1=GameMapUtil.getTilePoint(arg1, arg2);
            var loc2=-1;
            while (loc2 <= 1) 
            {
                loc3 = -3;
                while (loc3 <= 3) 
                {
                    loc4 = loc1.x + loc2;
                    loc5 = loc1.y + loc3;
                    loc6 = GameMapUtil.getPointValue(loc4, loc5);
                    if (!MapNodeType.isTowerArea(loc6)) 
                    {
                        return false;
                    }
                    ++loc3;
                }
                ++loc2;
            }
            return true;
        }

        /* internal  */isOtherTowerAround(arg1: Object /* flash.geom.Point */, arg2: int): Boolean
        {
            var loc3=null;
            var loc4=null;
            var loc1=GameMapUtil.getTilePoint(arg1.x, arg1.y);
            var loc2=ThingUtil.entityUtil.getEntityByRangle(SceneRange.display, EntityType.Boss, false);
            var loc5=0;
            var loc6=loc2;
            for(loc3 of loc6) 
            {
                if (!BossRule.isDefenceTower(loc3.bossInfo.type)) 
                {
                    continue;
                }
                loc4 = loc3.entityInfo.entityInfo.points[0];
                if (!(Math.abs(loc4.x - loc1.x) <= 2 && Math.abs(loc4.y - loc1.y) <= 5)) 
                {
                    continue;
                }
                return true;
            }
            return false;
        }

        /* internal  */get bodyPlayer(): SWFPlayer
        {
            if (this._bodyPlayer == null) 
            {
                this._bodyPlayer = new SWFPlayer();
            }
            return this._bodyPlayer;
        }

        /* internal  */get towerRoundEffect(): Object /* flash.display.Sprite */
        {
            var loc1=0;
            if (this._towerRoundEffect == null) 
            {
                this._towerRoundEffect = new Object /* flash.display.Sprite */();
                loc1 = GameMapUtil.tileWidth * 1.5;
                this._towerRoundEffect.graphics.beginFill(2293538, 0.45);
                this._towerRoundEffect.graphics.drawRect(-loc1, -loc1, loc1 * 2, loc1 * 2);
                this._towerRoundEffect.graphics.endFill();
                this._towerRoundEffect.mouseEnabled = false;
                this._towerRoundEffect.cacheAsBitmap = true;
            }
            return this._towerRoundEffect;
        }

        /* internal  */get towerAttackRound(): Object /* flash.display.Sprite */
        {
            if (this._towerAttackRound == null) 
            {
                this._towerAttackRound = new Object /* flash.display.Sprite */();
                this._towerAttackRound.mouseEnabled = false;
            }
            return this._towerAttackRound;
        }

        public startBuildControl(arg1: TCrossDefenseUpgrade): void
        {
            Cache.instance.crossDefenceCopy.isBuilding = true;
            this._buidingLayer = Game.scene.bulidlayer;
            this._buildingData = arg1;
            if (this._building) 
            {
                return;
            }
            this._building = true;
            flash.ui.Mouse.hide();
            var loc1=BossConfig.instance.getInfoByCode(arg1.preBoss);
            this.bodyPlayer.load(ModelUtil.getModelSWF(loc1.modelId), ModelType.Monster, loc1);
            this.bodyPlayer.sceneX = Game.scene.mouseX;
            this.bodyPlayer.sceneY = Game.scene.mouseY;
            this.redrawAttackRound(Cache.instance.crossDefenceCopy.getTowerAttackDistance(arg1));
            this._buidingLayer.addChildAt(this.towerRoundEffect, 0);
            this._buidingLayer.addChildAt(this.towerAttackRound, 0);
            this._buidingLayer.addChild(this.bodyPlayer);
            this.addEventListener();
            return;
        }

        public startGuideBuildControl(arg1: TCrossDefenseUpgrade): void
        {
            Cache.instance.crossDefenceCopy.isBuilding = true;
            this._buidingLayer = LayerManager.highestLayer;
            flash.ui.Mouse.hide();
            var loc1=BossConfig.instance.getInfoByCode(arg1.preBoss);
            this.bodyPlayer.load(ModelUtil.getModelSWF(loc1.modelId), ModelType.Monster, loc1);
            this.bodyPlayer.sceneX = this._buidingLayer.mouseX;
            this.bodyPlayer.sceneY = this._buidingLayer.mouseY;
            this.redrawAttackRound(Cache.instance.crossDefenceCopy.getTowerAttackDistance(arg1));
            this._buidingLayer.addChild(this.towerRoundEffect);
            this._buidingLayer.addChild(this.towerAttackRound);
            this._buidingLayer.addChild(this.bodyPlayer);
            this._buidingLayer.addEventListener(flash.events.MouseEvent.MOUSE_MOVE, this.mouseGuideMoveHandler);
            return;
        }

        public endGuideBuildControl(): void
        {
            flash.ui.Mouse.show();
            DisplayUtil.removeMe(this._bodyPlayer);
            DisplayUtil.removeMe(this._towerRoundEffect);
            DisplayUtil.removeMe(this._towerAttackRound);
            this._buidingLayer.removeEventListener(flash.events.MouseEvent.MOUSE_MOVE, this.mouseGuideMoveHandler);
            return;
        }

        /* internal  */mouseGuideMoveHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            this.bodyPlayer.sceneX = this._buidingLayer.mouseX;
            this.bodyPlayer.sceneY = this._buidingLayer.mouseY;
            this.towerRoundEffect.x = this._buidingLayer.mouseX;
            this.towerRoundEffect.y = this._buidingLayer.mouseY;
            this.towerAttackRound.x = this._buidingLayer.mouseX;
            this.towerAttackRound.y = this._buidingLayer.mouseY;
            if (!this.isTowerPoint(Game.scene.mouseX, Game.scene.mouseY) || this.isOtherTowerAround(new Object /* flash.geom.Point */(Game.scene.mouseX, Game.scene.mouseY), GameConst.CrossDefenseTowerDistance)) 
            {
                this.towerRoundEffect.filters = [FilterConst.colorFilterRed];
            }
            else 
            {
                this.towerRoundEffect.filters = [];
            }
            return;
        }

        public isBuildingSameTower(arg1: TCrossDefenseUpgrade): Boolean
        {
            return this._buildingData == arg1 && this._building;
        }

        public endBuildControl(): void
        {
            this.removeEventListener();
            this._building = false;
            flash.ui.Mouse.show();
            DisplayUtil.removeMe(this._bodyPlayer);
            DisplayUtil.removeMe(this._towerRoundEffect);
            DisplayUtil.removeMe(this._towerAttackRound);
            Cache.instance.crossDefenceCopy.isBuilding = false;
            return;
        }

        public get isBuilding(): Boolean
        {
            return this._building;
        }

        /* internal  */redrawAttackRound(arg1: int): void
        {
            if (this._towerAttackRound == null) 
            {
                this._towerAttackRound = new Object /* flash.display.Sprite */();
                this._towerAttackRound.mouseEnabled = false;
            }
            DisplayUtil.drawFadingEllipse(this._towerAttackRound, 12048540, arg1, arg1, 0.06, 0.65);
            this._towerAttackRound.cacheAsBitmap = true;
            return;
        }

        public showUpdateMenu(arg1: MonsterPlayer, arg2: CrossDefenceTowerInfo, arg3: Boolean=true): void
        {
            if (this._upgrater == null) 
            {
                this._upgrater = new CrossDefenceCopyUpgradeMenu();
            }
            this._clickTimes = 0;
            this._upgrater.updateView(arg1, arg2, arg3);
            if (this._upgrater.parent == null) 
            {
                LayerManager.uiLayer.addChild(this._upgrater);
            }
            var loc1=arg1.entityInfo.entityInfo.points[0];
            var loc2=GameMapUtil.getPixelPoint(loc1.x, loc1.y);
            this.towerAttackRound.x = loc2.x;
            this.towerAttackRound.y = loc2.y;
            this.redrawAttackRound(Cache.instance.crossDefenceCopy.getTowerAttackDistance(arg2.config));
            this._buidingLayer.addChildAt(this.towerAttackRound, 0);
            this._mouseClickTime = 0;
            Game.scene.removeEventListener(flash.events.MouseEvent.CLICK, this.clickSceneWhenUpgradeMenu);
            Game.scene.addEventListener(flash.events.MouseEvent.CLICK, this.clickSceneWhenUpgradeMenu);
            return;
        }

        public updateUpdateMenuWhenLevelUp(arg1: CrossDefenceTowerInfo): void
        {
            this._upgrater.reflashView(arg1);
            return;
        }

        public reflashUpdateMenu(): void
        {
            if (this._upgrater == null || this._upgrater.parent == null) 
            {
                return;
            }
            this._upgrater.doReflash();
            return;
        }

        public hideUpdateMenu(): void
        {
            this._mouseClickTime = 0;
            DisplayUtil.removeMe(this._upgrater);
            DisplayUtil.removeMe(this._towerAttackRound);
            Game.scene.removeEventListener(flash.events.MouseEvent.CLICK, this.clickSceneWhenUpgradeMenu);
            return;
        }

        public getCurMenu(): CrossDefenceCopyUpgradeMenu
        {
            return this._upgrater;
        }

        /* internal  */addEventListener(): void
        {
            Game.scene.addEventListener(flash.events.MouseEvent.CLICK, this.clickbuildHandler, false, 2048);
            Game.scene.addEventListener(flash.events.MouseEvent.MOUSE_MOVE, this.mouseMoveHandler);
            return;
        }

        /* internal  */removeEventListener(): void
        {
            Game.scene.removeEventListener(flash.events.MouseEvent.CLICK, this.clickbuildHandler);
            Game.scene.removeEventListener(flash.events.MouseEvent.MOUSE_MOVE, this.mouseMoveHandler);
            return;
        }

        /* internal  */clickSceneWhenUpgradeMenu(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1;
            var loc2=((loc1 = this)._mouseClickTime + 1);
            loc1._mouseClickTime = loc2;
            if (this._mouseClickTime < 2) 
            {
                return;
            }
            this.hideUpdateMenu();
            arg1.stopImmediatePropagation();
            return;
        }

        /* internal  */clickbuildHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            this.endBuildControl();
            if (!this.isTowerPoint(Game.scene.mouseX, Game.scene.mouseY)) 
            {
                MsgManager.showRollTipsMsg(Language.getString(80304));
                return;
            }
            if (this.isOtherTowerAround(new Object /* flash.geom.Point */(Game.scene.mouseX, Game.scene.mouseY), GameConst.CrossDefenseTowerDistance)) 
            {
                MsgManager.showRollTipsMsg(Language.getStringByParam(80305, GameConst.CrossDefenseTowerDistance));
                return;
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.ReqCrossDefenceCopyBuildTower, this._buildingData));
            return;
        }

        /* internal */ /* var */_building: Boolean=false;

        /* internal */ /* var */_buildingData: TCrossDefenseUpgrade;

        /* internal */ /* var */_upgrater: CrossDefenceCopyUpgradeMenu;

        /* internal */ /* var */_clickTimes: int;

        /* internal */ /* var */_towerRoundEffect: Object /* flash.display.Sprite */;

        /* internal */ /* var */_towerAttackRound: Object /* flash.display.Sprite */;

        /* internal */ /* var */_buidingLayer: Object /* flash.display.DisplayObjectContainer */;

        /* internal */ /* var */_mouseClickTime: int;

        /* internal */ /* var */_bodyPlayer: SWFPlayer;
    }
